#!/usr/local/bin/perl
use warnings;
use Card;
use DefaultDeck;

$deck = new DefaultDeck();
sub main()
{
$wins = 0;

$losses = 0;

$continue = 0;
$wins = 0;

$losses = 0;

$continue = 1;

print ""."\n";

while (($continue==1)) {
if (play()){{
$wins = ($wins+1);
print "You win."."\n";
}
}
 else {{
$losses = ($losses+1);
print "You lose."."\n";
}
}
print "You have won ";
print $wins;
print " game(s) and lost ";
print $losses;
print " game(s)."."\n";
print "Would you like to continue playing (1) or stop (0)?"."\n";
$continue = <STDIN>;
while ((($continue*$continue)!=$continue)) {
print "Please respond with 0 or 1."."\n";
$continue = <STDIN>;
}

print ""."\n";
}


print "Your final score is ";

print $wins;

print " win(s) and ";

print $losses;

print " loss(es)."."\n";
}
sub value {
$name = $_[0];
$value = substr $name, 1;
if ($value eq "A")
{
return 1;
}
else
{if ($value eq "J")
{
return 11;
}
else
{
if ($value eq "Q")
{
return 12;
}
else
{
if ($value eq "K")
{
return 13;
}
else
{
return $value;
}
}
}
}
}

sub play()
{
$myfirst = "";

$myfirstvalue = 0;

$mysecond = "";

$mysecondvalue = 0;

$dealerfirst = "";

$dealerfirstvalue = 0;

$dealersecond = "";

$dealersecondvalue = 0;

$myscore = 0;

$dealerscore = 0;

$myaces = 0;

$dealeraces = 0;

$mynext = "";

$mynextvalue = 0;

$dealernext = "";

$dealernextvalue = 0;

$myinput = 0;

$dealerinput = 0;
$myfirst = $deck->draw()->getname();

$myfirstvalue = value($myfirst);

if (($myfirstvalue==1)){{
$myfirstvalue = 11;
$myaces = ($myaces+1);
}
}
 else {if (((($myfirstvalue==11)||($myfirstvalue==12))||($myfirstvalue==13)))
 {{
$myfirstvalue = 10;
}
}
}

print "You drew ";

print $myfirst;

print " with value ";

print $myfirstvalue;

print "."."\n";

$dealerfirst = $deck->draw()->getname();

$dealerfirstvalue = value($dealerfirst);

if (($dealerfirstvalue==1)){{
$dealerfirstvalue = 11;
$dealeraces = ($dealeraces+1);
}
}
 else {if (((($dealerfirstvalue==11)||($dealerfirstvalue==12))||($dealerfirstvalue==13)))
 {{
$dealerfirstvalue = 10;
}
}
}

print "The dealer drew a card."."\n";

$mysecond = $deck->draw()->getname();

$mysecondvalue = value($mysecond);

if (($mysecondvalue==1)){{
$mysecondvalue = 11;
$myaces = ($myaces+1);
}
}
 else {if (((($mysecondvalue==11)||($mysecondvalue==12))||($mysecondvalue==13)))
 {{
$mysecondvalue = 10;
}
}
}

print "You drew ";

print $mysecond;

print " with value ";

print $mysecondvalue;

print "."."\n";

$dealersecond = $deck->draw()->getname();

$dealersecondvalue = value($dealersecond);

if (($dealersecondvalue==1)){{
$dealersecondvalue = 11;
$dealeraces = ($dealeraces+1);
}
}
 else {if (((($dealersecondvalue==11)||($dealersecondvalue==12))||($dealersecondvalue==13)))
 {{
$dealersecondvalue = 10;
}
}
}

print "The dealer drew ";

print $dealersecond;

print " with value ";

print $dealersecondvalue;

print "."."\n";

$myscore = ($myfirstvalue+$mysecondvalue);

$myinput = 1;

while (($myinput==1)) {
if (($myscore>21))
 {{
if (($myaces>0)){{
$myaces = ($myaces-1);
$myscore = ($myscore-10);
}
}
 else {{
print "Your current score is ";
print $myscore;
print "."."\n";
print "You have gone over 21."."\n";
print ""."\n";
}
}
}
}
print "Your current score is ";
print $myscore;
print "."."\n";
print "What would you like to do? Hit (1) or Stay (0)?"."\n";
$myinput = <STDIN>;
while ((($myinput*$myinput)!=$myinput)) {
print "Please respond with 0 or 1."."\n";
$myinput = <STDIN>;
}

print ""."\n";
if (($myinput==1))
 {{
$mynext = $deck->draw()->getname();
$mynextvalue = value($mynext);
if (($mynextvalue==1)){{
$myaces = ($myaces+1);
$mynextvalue = 11;
}
}
 else {if (((($mynextvalue==11)||($mynextvalue==12))||($mynextvalue==13)))
 {{
$mynextvalue = 10;
}
}
}
print "You drew ";
print $mynext;
print " with value ";
print $mynextvalue;
print "."."\n";
$myscore = ($myscore+$mynextvalue);
}
}
}


$dealerscore = ($dealerfirstvalue+$dealersecondvalue);

$dealerinput = 1;

while (($dealerinput==1)) {
if (($dealerscore>21))
 {{
if (($dealeraces>0))
 {{
$dealeraces = ($dealeraces-1);
$dealerscore = ($dealerscore-10);
}
}
}
}
if (($dealerscore<17)){{
$dealerinput = 1;
}
}
 else {{
$dealerinput = 0;
}
}
if (($dealerinput==1))
 {{
$dealernext = $deck->draw()->getname();
$dealernextvalue = value($dealernext);
if (($dealernextvalue==1)){{
$dealeraces = ($dealeraces+1);
$dealernextvalue = 11;
}
}
 else {if (((($dealernextvalue==11)||($dealernextvalue==12))||($dealernextvalue==13)))
 {{
$dealernextvalue = 10;
}
}
}
$dealerscore = ($dealerscore+$dealernextvalue);
}
}
}


print "Your score is ";

print $myscore;

print " and the dealer's score is ";

print $dealerscore;

print "."."\n";

if ((($myscore>21)||(($dealerscore<=21)&&($dealerscore>=$myscore)))){{
return 0;
}
}
 else {{
return 1;
}
}
}
print main()."\n";
